package com.nhncorp.pleiades.client;

import java.lang.reflect.Constructor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nhncorp.pleiades.conf.PleiadesConfiguration;

public class EventServiceClientFactory {

  public static final Log LOG = LogFactory.getLog(EventServiceClientFactory.class
      .getName());

  public static EventService newInstance(PleiadesConfiguration conf) {
    try {
      String clientInstance = conf.get("pleiades.client.eventServiceClient",
          EventServiceClient.class.getCanonicalName());
      Class<?> cl = Class.forName(clientInstance);
      Class[] argType = new Class[] { PleiadesConfiguration.class };
      Constructor con = cl.getConstructor(argType);
      if (con == null) {
        LOG.fatal("Not found the matched constructor");
        return null;
      }
      Object[] initArg = new Object[] { conf };

      return (EventService) con.newInstance(initArg);

    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
  
  public static EventService newInstance(PleiadesUser userInfo, PleiadesConfiguration conf) {
    try {
      String clientInstance = conf.get("pleiades.client.eventServiceClient",
          EventServiceClient.class.getCanonicalName());
      Class<?> cl = Class.forName(clientInstance);
      Class[] argType = new Class[] { PleiadesUser.class, PleiadesConfiguration.class };
      Constructor con = cl.getConstructor(argType);
      if (con == null) {
        LOG.fatal("Not found the matched constructor");
        return null;
      }
      Object[] initArg = new Object[] { userInfo, conf };
      
      return (EventService) con.newInstance(initArg);

    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

}
